package leetcode.L500;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class EX78 {
    public List<List<Integer>> subsets(int[] nums) {

        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        Arrays.sort(nums);
        int[] used = new int[nums.length];
        dfs(nums,path,res,0,used);
        return res;

    }

    private void dfs(int[] nums, List<Integer> path, List<List<Integer>> res,int now,int[] used) {
        res.add(new ArrayList<>(path));
        for (int i = now; i < nums.length ; i++) {
            if(used[i]==0) {
                path.add(nums[i]);
                used[i]=1;
                dfs(nums, path, res, i + 1, used);
                path.remove(path.size() - 1);
                used[i] = 0;
            }
        }
    }
    public static void main(String[] args) {
        int[] nums = {1, 2, 2};
        EX78 solution = new EX78();
        List<List<Integer>> lists = solution.subsets(nums);
        System.out.println(lists);
    }
}
